<template>
  <div class="login-container">
    <div class="login-form">
      <img src="../../assets/logo.595745bd.png" class="login-logo">
      <el-form
        ref="loginForm"
        class="login-form"
        :model="loginForm"
        :rules="rules"
        label-position="left"
      >
        <el-form-item prop="mobile">
          <span class="svg-container">
            <i class="el-icon-mobile-phone" />
          </span>
          <el-input v-model="loginForm.mobile" placeholder="请输入账号" />
        </el-form-item>
        <el-form-item prop="password">
          <span class="svg-container">
            <i class="el-icon-lock" />
          </span>
          <el-input
            ref="pwdInput"
            v-model="loginForm.password"
            :type="passwordType"
            placeholder="请输入密码"
          />
          <span class="svg-container" @click="showPwd">
            <svg-icon :icon-class="passwordType==='password'? 'eye' : 'eye-open'" />
          </span>
        </el-form-item>
        <el-form-item prop="yzma">
          <span class="svg-container">
            <i class="el-icon-trophy" />
          </span>
          <el-input
            v-model="loginForm.yzma"
            placeholder="请输入验证码"
          >
            <template #suffix>
              <img :src="logToke" class="yanzma" @click="logTokes">
            </template>
          </el-input>
        </el-form-item>
        <el-button
          class="loginBtn"
          style="margin-bottom:30px;"
          :loading="loading"
          @click="login"
        >登录</el-button>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
      loading: false,
      passwordType: 'password',
      loginForm: {
        mobile: 'admin',
        password: 'admin',
        yzma: ''
      },
      logToke: '/api/user-service/user/imageCode/',
      rules: {
        mobile: [
          { required: true, message: '请输入账号', trigger: 'change' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'change' }
        ],
        yzma: [
          { required: true, message: '请输入验证码', trigger: 'change' }
        ]
      }
    }
  },
  created() {
    this.logTokes()
  },
  methods: {
    showPwd() {
      this.passwordType === 'password' ? this.passwordType = ''
        : this.passwordType = 'password'
      this.$nextTick(() => {
        this.$refs.pwdInput.focus()
      })
    },
    logTokes() {
      this.sun = ''
      for (var i = 0; i < 1; i++) {
        // var sun = Math.round(Math.random() * 10)
        this.sun += Math.round(Math.random() * 10)
      }
      this.logToke = `/api/user-service/user/imageCode/${this.sun}`
    },
    async login() {
      try {
        await this.$refs.loginForm.validate()
        this.loading = true
        await this.$store.dispatch('user/login', this.loginForm)
        this.$router.push('/')
      } catch (e) {
        console.log(e)
      } finally {
        this.loading = false
      }
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#999;
$light_gray:#999;
$cursor: #000;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  position: relative;
  .login-logo{
    position: absolute;
    width: 96px;
    height: 96px;
    z-index: 999;
    top: -46px;
    left: 50%;
    margin-left: -48px;
}
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;
    position: relative;
    .yanzma{
      background-color: #000;
      height:53px;
      width:130px;
      position:absolute;
      left: -87px;
      top: -1px;
    }

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(0, 0, 0, 0.1);
    background: #fff;
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#000;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;
  background-image: url('~@/assets/background.be4fae7d.png');
  background-position: center;
  .login-form {
   position: absolute;
    width: 518px;
    height: 388px;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    padding: 76px 35px 0;
    background: #fff;
    border-radius: 10px;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
  .loginBtn{
    width: 100%;
    height: 52px;
    background: linear-gradient(262deg,#2e50e1,#6878f0);
    opacity: .91;
    border-radius: 8px;
    color: #fff;
    text-shadow: 0 7px 22px #cfcfcf;
  }
}
</style>
